/*
 * Created by JFormDesigner on Sat Mar 26 12:07:14 CST 2022
 */

package com.wangcanxuan.www.view.user;

import com.wangcanxuan.www.controller.Impl.UserControllerImpl;
import com.wangcanxuan.www.controller.UserController;
import com.wangcanxuan.www.entity.dto.ResultInfo;
import com.wangcanxuan.www.entity.po.User;
import com.wangcanxuan.www.proxy.ControllerProxy;
import com.wangcanxuan.www.util.ConnectionPoolUtil;
import com.wangcanxuan.www.util.ValidateUtil;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import static com.wangcanxuan.www.entity.dto.ResultConstants.*;

/**
 * 登录界面
 *
 * @author unknown
 * @date 2022/04/01
 */
public class LoginOnView extends JFrame {
    private final UserController userController;

    public LoginOnView() {
        userController = ControllerProxy.getProxyInstance(UserControllerImpl.class);
        initComponents();
        setVisible(true);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }


    private void loginOn(ActionEvent e) {
        String userName = this.userNameText.getText();
        String password = new String(this.passwordText.getPassword());
        if (ValidateUtil.strIsNullOrEmpty(userName)) {
            JOptionPane.showMessageDialog(null, "用户名不能为空！");
            return;
        }
        if (ValidateUtil.strIsNullOrEmpty(password)) {
            JOptionPane.showMessageDialog(null, "密码不能为空！");
        }
        ResultInfo<User> login = userController.login(userName, password);
        int code = login.getCode();
        String message = login.getMessage();
        User user = login.getObj();
        if (code == SUCCESS_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            new MainView(user);

            dispose();
        }
        if (code == REJECT_REQUEST_CODE.getCode()) {
            JOptionPane.showMessageDialog(null, message);
            passwordText.setText("");
        }

    }

    private void register(ActionEvent e) {
        new RegisterView();
        dispose();
    }

    private void exit(ActionEvent e) {
        // TODO add your code here
        //这里写对话框
        if (JOptionPane.showConfirmDialog(null
                , "退出", "提示 ", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
            dispose();
            ConnectionPoolUtil.getInstance().closeConnectionPool();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
            System.exit(0);
        }

    }

    private void findPassword(ActionEvent e) {
        // TODO add your code here
        new FindPasswordView();
        dispose();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        userNameText = new JTextField();
        loginOnButton = new JButton();
        passwordText = new JPasswordField();
        label2 = new JLabel();
        label3 = new JLabel();
        registerButton = new JButton();
        label4 = new JLabel();
        exitButton = new JButton();
        findPasswordButton = new JButton();
        label5 = new JLabel();
        label1 = new JLabel();
        label6 = new JLabel();

        //======== this ========
        setTitle("Dream\u767b\u5f55\u754c\u9762");
        setIconImage(new ImageIcon(LoginOnView.class.getClassLoader().getResource("Main.png")).getImage());
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.add(userNameText);
        userNameText.setBounds(265, 120, 146, userNameText.getPreferredSize().height);

        //---- loginOnButton ----
        loginOnButton.setText("\u767b\u5f55");
        loginOnButton.addActionListener(e -> loginOn(e));
        contentPane.add(loginOnButton);
        loginOnButton.setBounds(270, 225, 110, 30);
        contentPane.add(passwordText);
        passwordText.setBounds(265, 165, 146, passwordText.getPreferredSize().height);

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d\uff1a");
        label2.setFont(new Font("\u534e\u6587\u65b0\u9b4f", Font.PLAIN, 16));
        label2.setForeground(Color.white);
        contentPane.add(label2);
        label2.setBounds(190, 120, label2.getPreferredSize().width, 23);

        //---- label3 ----
        label3.setText("\u5bc6 \u7801\uff1a");
        label3.setFont(new Font("\u534e\u6587\u65b0\u9b4f", Font.PLAIN, 16));
        label3.setForeground(Color.white);
        contentPane.add(label3);
        label3.setBounds(195, 165, 65, 26);

        //---- registerButton ----
        registerButton.setText("\u6ce8\u518c");
        registerButton.addActionListener(e -> register(e));
        contentPane.add(registerButton);
        registerButton.setBounds(new Rectangle(new Point(10, 325), registerButton.getPreferredSize()));

        //---- label4 ----
        label4.setText("\u8fd8\u6ca1\u6709\u7528\u6237\uff1f\u8d76\u7d27\u6ce8\u518c\u8fdb\u5165\u767d\u65e5\u68a6\u5427");
        label4.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 12));
        label4.setForeground(Color.white);
        contentPane.add(label4);
        label4.setBounds(new Rectangle(new Point(5, 305), label4.getPreferredSize()));

        //---- exitButton ----
        exitButton.setText("\u9000\u51fa\u7a0b\u5e8f");
        exitButton.addActionListener(e -> exit(e));
        contentPane.add(exitButton);
        exitButton.setBounds(new Rectangle(new Point(545, 10), exitButton.getPreferredSize()));

        //---- findPasswordButton ----
        findPasswordButton.setText("\u627e\u56de\u5bc6\u7801");
        findPasswordButton.addActionListener(e -> findPassword(e));
        contentPane.add(findPasswordButton);
        findPasswordButton.setBounds(555, 325, 105, findPasswordButton.getPreferredSize().height);

        //---- label5 ----
        label5.setText("\u5fd8 \u8bb0 \u5bc6 \u7801 \uff1f");
        label5.setForeground(Color.white);
        contentPane.add(label5);
        label5.setBounds(565, 305, 85, label5.getPreferredSize().height);

        //---- label1 ----
        label1.setText("\u767d\u65e5\u68a6");
        label1.setFont(new Font("\u534e\u6587\u65b0\u9b4f", Font.PLAIN, 28));
        label1.setEnabled(false);
        label1.setForeground(Color.white);
        contentPane.add(label1);
        label1.setBounds(280, 25, 130, 50);

        //---- label6 ----
        label6.setIcon(new ImageIcon(getClass().getResource("/LoginOn.png")));
        contentPane.add(label6);
        label6.setBounds(0, 5, 665, 355);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JTextField userNameText;
    private JButton loginOnButton;
    private JPasswordField passwordText;
    private JLabel label2;
    private JLabel label3;
    private JButton registerButton;
    private JLabel label4;
    private JButton exitButton;
    private JButton findPasswordButton;
    private JLabel label5;
    private JLabel label1;
    private JLabel label6;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
